package com.qiaofang.controller;

import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.response.SuccessfulResponseFactory;
import com.qiaofang.common.utils.UploadFileUtil;
import com.qiaofang.request.ListCityRequest;
import com.qiaofang.request.ListZoneRequest;
import com.qiaofang.request.RefreshAdviserByPartnerIdRequest;
import com.qiaofang.request.RefreshAdviserRequest;
import com.qiaofang.response.ListCityResponse;
import com.qiaofang.response.ListProvinceResponse;
import com.qiaofang.response.ListZoneResponse;
import com.qiaofang.response.UploadFileResponse;
import com.qiaofang.service.AreaService;
import com.qiaofang.service.PremisesService;
import com.qiaofang.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@Api(tags = {"通用接口"})
@RequestMapping(value = "/common", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class CommonController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private PremisesService premisesService;

    @PostMapping(value = "/uploadFile")
    @ApiOperation(value = "上传文件")
    public UploadFileResponse uploadFile(@RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                         @RequestParam(value = "type", required = false) String type) {
        UploadFileResponse response = new UploadFileResponse();
        String url = UploadFileUtil.uploadFile(type, multipartFile);
        response.setData(url);
        return response;
    }

    @PostMapping(value = "/uploadWxJs")
    @ApiOperation(value = "上传微信配置")
    public CommonResponse uploadWxJs(@RequestParam(value = "file", required = false) MultipartFile multipartFile) {
        UploadFileUtil.uploadWxJs(multipartFile);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/listProvince")
    @ApiOperation(value = "省份列表")
    public ListProvinceResponse listProvince() {
        ListProvinceResponse response = new ListProvinceResponse();
        response.setData(areaService.listProvince());
        return response;
    }

    @PostMapping(value = "/listCity")
    @ApiOperation(value = "城市列表")
    public ListCityResponse listCity(@RequestBody @Valid ListCityRequest request) {
        ListCityResponse response = new ListCityResponse();
        response.setData(areaService.listCity(request.getProvinceId()));
        return response;
    }

    @PostMapping(value = "/listZone")
    @ApiOperation(value = "区列表")
    public ListZoneResponse listZone(@RequestBody @Valid ListZoneRequest request) {
        ListZoneResponse response = new ListZoneResponse();
        response.setData(areaService.listZone(request.getCityId()));
        return response;
    }

    @PostMapping(value = "/clearInvalidReport")
    @ApiOperation(value = "清空失效报备")
    public CommonResponse clearInvalidReport(@RequestBody @Valid Long premisesId) {
        reportService.clearInvalidReportByPremisesId(premisesId);
        return SuccessfulResponseFactory.getSuccessResponse();
    }

    @PostMapping(value = "/refreshAdviser")
    @ApiOperation(value = "刷新楼盘置业顾问关联关系")
    public CommonResponse refreshAdviser(@RequestBody @Valid RefreshAdviserRequest request) {
        CommonResponse response = premisesService.refreshAdviser(request.getPremisesId());
        return response;
    }
    
    @PostMapping(value = "/refreshAdviserByPartnerId")
    @ApiOperation(value = "刷新某个人员的置业顾问相关报备联系")
    public CommonResponse refreshAdviserByPartnerId(@RequestBody @Valid RefreshAdviserByPartnerIdRequest request) {
        CommonResponse response = premisesService.refreshAdviserByPartnerId(request.getPartnerId());
        return response;
    }
}
